package com.itheima.stargraph.ollama.service;

import com.openai.client.OpenAIClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * openAI翻译服务
 * 根据提示词，直接翻译为英文
 *
 * @Author mr.Du
 * @Date 2025/6/11 2025/6/11
 */
@Component
public class OllamaService {

    @Autowired
    private OpenAIClient openAIClient;

    @Value("${ollama.model}")
    private String model;

    public String translate(String prompt){
        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addSystemMessage("你是一个专业的翻译助手，专注于中英互译。接下来用户提问的内容都要用英文回答！")
                .addUserMessage(prompt)
                .model(model)
                .build();

        //发起对话接口调用
        ChatCompletion chatCompletion = openAIClient.chat().completions().create(params);

        //获取调用接口返回的结果（翻译的英文结果）
        String result = chatCompletion.choices().get(0).message().content().orElse(null);
        return result;
    }

}
